<?php

namespace tencentLive;

use think\Exception;

class tencentapi
{
    protected $appkey = '14a82589b03e42e79d76831b58b21a82';
    protected $accesstoken = 'fd3a79dc961f46ac93acd9742adb260a';
    protected $baseUrl = 'https://gw.tvs.qq.com';
    protected $common = '';

    public function __construct($appkey = "", $accesstoken = "")
    {
        $appkey ? $this->appkey = $appkey : "";
        $accesstoken ? $this->accesstoken = $accesstoken : "";
    }

    //获取加密密钥
    public function getSignature()
    {
        $data_one = ['appkey' => $this->appkey, 'timestamp' => time()];
        ksort($data_one);
        $data = http_build_query($data_one);
        $this->signature = base64_encode(hash_hmac('sha256', $data, $this->accesstoken, true));
        $data_one['signature'] = $this->signature;
        $this->common = http_build_query($data_one);
    }

    //客户资源查询接口
    public function getResource($getallresource = true)
    {
        $param = ['Header' => (object)[], 'Payload' => ['GetAllResource' => $getallresource]];
        $this->getSignature();
        $url = '/v2/ivh/videomaker/broadcastservice/getresource';
        $url = $this->baseUrl . $url . '?' . $this->common;
        return httpRequest($url, json_encode($param));
    }

    // 试听
    public function tts($VirtualmanKey, $TimbreKey, $InputSsml)
    {
        $param = ['Header' => (object)[], 'Payload' => ['VirtualmanKey' => $VirtualmanKey, 'TimbreKey' => $TimbreKey, 'InputSsml' => $InputSsml, 'Speed' => 1.0]];
        $this->getSignature();
        $url = '/v2/ivh/videomaker/broadcastservice/tts';
        $url = $this->baseUrl . $url . '?' . $this->common;
        return httpRequest($url, json_encode($param));
    }


    //客户资源查询主播接口
    public function getAnchor($getallresource = false, $virtualmantypecode = 'real_man_3d')
    {
        $param = ['Header' => (object)[], 'Payload' => ['GetAllResource' => $getallresource, 'VirtualmanTypeCode' => $virtualmantypecode]];
        $this->getSignature();
        $url = '/v2/ivh/videomaker/broadcastservice/getanchor';
        $url = $this->baseUrl . $url . '?' . $this->common;
        return httpRequest($url, json_encode($param));
    }

    //查询某个主播下所有的形象
    public function getResourceByAnchor($getallresource = false, $anchorcode = 'real_man_3d')
    {
        $param = ['Header' => (object)[], 'Payload' => ['GetAllResource' => $getallresource, 'AnchorCode' => $anchorcode]];
        $this->getSignature();
        $url = '/v2/ivh/videomaker/broadcastservice/getresourcebyanchor';
        $url = $this->baseUrl . $url . '?' . $this->common;
        return httpRequest($url, json_encode($param));
    }

    //查看是合成进度
    public function getProgress($TaskId)
    {
        if (empty($TaskId)) throw new Exception('TaskId必传不能为空');
        $param = ['Header' => (object)[], 'Payload' => ['TaskId' => $TaskId]];
        $this->getSignature();
        $url = '/v2/ivh/videomaker/broadcastservice/getprogress';
        $url = $this->baseUrl . $url . '?' . $this->common;
        return httpRequest($url, json_encode($param));

    }

    //合成视频
    public function videoMakeAdvanced($virtualmanKey, $inputssml, $speechparam, $driverType = 'Text', $inputAudioUrl = '', $callbackurl = '')
    {
        if (empty($virtualmanKey)) throw new Exception('VirtualmanKey必传不能为空');;
        if ($driverType == 'Text' && empty($inputssml)) throw new Exception('InputSsmly必传不能为空');
        if ($driverType == 'OriginalVoice' && empty($inputAudioUrl)) throw new Exception('音频url不能为空');
        if (empty($speechparam)) throw new Exception('speechparam必传不能为空');

        $param = ['Header' => (object)[], 'Payload' => ['VirtualmanKey' => $virtualmanKey, 'InputSsml' => $inputssml, 'SpeechParam' => $speechparam, 'DriverType' => $driverType, 'InputAudioUrl' => $inputAudioUrl, 'CallbackUrl' => $callbackurl]];
        $this->getSignature();
        $url = '/v2/ivh/videomaker/broadcastservice/videomakeadvanced';
        $url = $this->baseUrl . $url . '?' . $this->common;
        return httpRequest($url, json_encode($param));
    }
}